package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.domain.system.SysRole;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.domain.system.SysUserRole;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.system.ISysRoleMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysRoleService;
import com.wejoy.video.middle.station.data.service.system.ISysUserRoleService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.system.AddRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统--角色表
 *
 * @author admin
 * @date 2021年10月19日 15:06:47
 */
@Service
public class SysRoleServiceImpl implements ISysRoleService {
    @Autowired
    private ISysRoleMapper sysRoleMapper;
    @Autowired
    private IIdWorkerService idWorkerService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    public void save(SysRole req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysRole req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return sysRoleMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(SysRole sysRole) {
        WJAssert.notNull(sysRole, "入参对象不能为空");
        WJAssert.notNull(sysRole.getId(), "id参数错误");
        SysRole where = new SysRole();
        where.setId(sysRole.getId());
        return sysRoleMapper.updateSelective(sysRole, where);
    }

    @Override
    public int getCount(SysRole sysRole) {
        checkStringLength(sysRole, true);
        return sysRoleMapper.getCount(sysRole);
    }

    @Override
    public SysRole getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysRole where = new SysRole();
        where.setId(id);
        where.setStatus(EnumStatus.NEW.getCode());
        return sysRoleMapper.getOne(where);
    }

    @Override
    public List<SysRole> getList(SysRole sysRole) {
        checkStringLength(sysRole, true);
        return sysRoleMapper.getList(sysRole);
    }

    @Override
    public List<SysRole> getPageList(SysRole sysRole) {
        checkStringLength(sysRole, true);
        return sysRoleMapper.getPageList(sysRole);
    }

    @Override
    public void audit(Long id, String operator) {
        SysRole req = new SysRole();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        SysRole req = new SysRole();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        SysRole req = new SysRole();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 获取当前登陆用户拥有绝角色
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysRole> getCurrentUserRole(Long userId) {
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setStatus(EnumStatus.NEW.getCode());
        sysUserRole.setSysUserId(userId);
        List<SysUserRole> sysUserRoleList = sysUserRoleService.getList(sysUserRole);
        if (CollectionUtils.isEmpty(sysUserRoleList)) {
            //如果角色为空，根据EVStype添加用户角色
            return null;
        }
        List<Long> sysRoleIdList = sysUserRoleList.stream().map(SysUserRole::getSysRoleId).collect(Collectors.toList());
        SysRole sysRole = new SysRole();
        sysRole.setSysRoleIdList(sysRoleIdList);
        List<SysRole> sysRoleList = sysRoleMapper.getList(sysRole);
        return sysRoleList;
    }

    /**
     * 获取当前登陆用户拥有绝角色
     *
     * @param sysUser
     * @return
     */
    @Override
    public List<SysRole> getCurrentUserRole(SysUser sysUser) {
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setStatus(EnumStatus.NEW.getCode());
        sysUserRole.setSysUserId(sysUser.getId());
        List<SysUserRole> sysUserRoleList = sysUserRoleService.getList(sysUserRole);
        if (CollectionUtils.isEmpty(sysUserRoleList)) {
            //如果角色为空，根据EVStype添加用户角色
            AddRoleVO addRoleVO = new AddRoleVO();
            addRoleVO.setNewRole(Collections.singletonList(sysUser.getType().equals("ordinary_user")?351918074250682369L:335227689252261888L));
            addRoleVO.setUserId(sysUser.getId());
            sysUserRoleService.addRole(addRoleVO,sysUser);
            sysUserRoleList = sysUserRoleService.getList(sysUserRole);
        }
        List<Long> sysRoleIdList = sysUserRoleList.stream().map(SysUserRole::getSysRoleId).collect(Collectors.toList());
        SysRole sysRole = new SysRole();
        sysRole.setSysRoleIdList(sysRoleIdList);
        List<SysRole> sysRoleList = sysRoleMapper.getList(sysRole);
        return sysRoleList;
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysRole sysRole, boolean nullAble) {
        WJAssert.notNull(sysRole, "入参对象不能为空");
        WJAssert.limitMaxLength(sysRole.getName(), nullAble, 100, "角色名称不合法!");
        WJAssert.limitMaxLength(sysRole.getDescription(), nullAble, 200, "描述不合法!");
        WJAssert.limitMaxLength(sysRole.getOperator(), true, 255, "操作人不合法!");
        WJAssert.limitMaxLength(sysRole.getRemark(), true, 255, "备注不合法!");
    }
}
